package com.whenjun.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ifootbook.util.common.BackJson;
import com.ifootbook.util.common.CallResult;
import com.whenjun.common.LoggerParent;
import com.whenjun.dto.base.BaseId;
import com.whenjun.dto.door.IscDoorDTO;
import com.whenjun.dto.door.IscDoorReq;
import com.whenjun.dto.isc.person.IscPersonReq;
import com.whenjun.dto.kq.*;
import com.whenjun.dto.person.IscPersonDTO;
import com.whenjun.entity.IscCamera;
import com.whenjun.entity.KqConfig;
import com.whenjun.enums.KqAlarmTypeEnum;
import com.whenjun.enums.KqDoorTypeEnum;
import com.whenjun.service.impl.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("kq")
@SaCheckPermission(value = "kq", orRole = "admin")
@Api(tags = "考勤管理")
@SaCheckLogin
public class KqController extends LoggerParent {

    @Autowired
    private KqDepartmentServiceImpl kqDepartmentService;

    @Autowired
    private KqDepartmentGroupServiceImpl kqDepartmentGroupService;

    @Autowired
    private KqDepartmentGroupPersonServiceImpl kqDepartmentGroupPersonService;

    @Autowired
    private KqConfigServiceImpl kqConfigService;

    @Autowired
    private IscDoorServiceImpl iscDoorService;

    @Autowired
    private KqLocationServiceImpl kqLocationService;

    @Autowired
    private KqLocationGroupServiceImpl kqLocationGroupService;

    @Autowired
    private KqLocationGroupDoorServiceImpl kqLocationGroupDoorService;

    @Autowired
    private KqAlarmServiceImpl kqAlarmService;

    @Autowired
    private KqServiceImpl kqService;

    @Autowired
    private KqLocationLeaveServiceImpl kqLocationLeaveService;

    @Autowired
    private IscCameraServiceImpl iscCameraService;

    // --------------    人员部门   --------------

    @ApiOperation("人员部门-获取")
    @PostMapping("KqDepartments")
    public CallResult<List<KqDepartmentDTO>> KqDepartments(@RequestBody(required = false) @Valid KqDepartmentReq kqDepartmentReq){
        return BackJson.success(kqDepartmentService.KqDepartments(kqDepartmentReq));
    }

    @ApiOperation("人员部门-新增")
    @PostMapping("KqDepartmentAdd")
    public CallResult<?> KqDepartmentAdd(@RequestBody(required = true) @Valid KqDepartmentAddReq kqDepartmentAddReq){
        kqDepartmentService.KqDepartmentAdd(kqDepartmentAddReq);
        return BackJson.success();
    }

    @ApiOperation("人员部门-删除")
    @PostMapping("KqDepartmentDel")
    public CallResult<?> KqDepartmentDel(@RequestBody(required = true) @Valid BaseId baseId){
        kqDepartmentService.KqDepartmentDel(baseId);
        return BackJson.success();
    }

    @ApiOperation("人员部门-修改")
    @PostMapping("KqDepartmentUpd")
    public CallResult<?> KqDepartmentUpd(@RequestBody(required = true) @Valid KqDepartmentUpdReq kqDepartmentUpdReq){
        kqDepartmentService.KqDepartmentUpd(kqDepartmentUpdReq);
        return BackJson.success();
    }


    // --------------   考勤班组管理  -------------

    @ApiOperation("考勤班组-获取")
    @PostMapping("KqGroups")
    public CallResult<List<KqDepartmentGroupDTO>> KqGroups(@RequestBody(required = false) @Valid KqGroupReq kqGroupReq){
        return BackJson.success(kqDepartmentGroupService.KqGroups(kqGroupReq));
    }

    @ApiOperation("考勤班组-新增")
    @PostMapping("KqGroupAdd")
    public CallResult<?> KqGroupAdd(@RequestBody(required = true) @Valid KqGroupAddReq kqGroupAddReq){
        kqDepartmentGroupService.KqGroupAdd(kqGroupAddReq);
        return BackJson.success();
    }

    @ApiOperation("考勤班组-删除")
    @PostMapping("KqGroupDel")
    public CallResult<?> KqGroupDel(@RequestBody(required = true) @Valid BaseId baseId){
        kqDepartmentGroupService.KqGroupDel(baseId);
        return BackJson.success();
    }

    @ApiOperation("考勤班组-修改")
    @PostMapping("KqGroupUpd")
    public CallResult<?> KqGroupUpd(@RequestBody(required = true) @Valid KqGroupUpdReq kqGroupUpdReq){
        kqDepartmentGroupService.KqGroupUpd(kqGroupUpdReq);
        return BackJson.success();
    }

    // --------------   考勤班组管理 end  -------------


    // --------------   考勤班组人员管理   --------------------
    @ApiOperation("考勤班组人员列表-获取（可按组）")
    @PostMapping("KqGroupPersons")
    public CallResult<List<IscPersonDTO>> KqGroupPersons(@RequestBody(required = true) @Valid KqGroupPersonReq kqGroupPersonReq){
        return BackJson.success(kqDepartmentGroupPersonService.KqGroupPersons(kqGroupPersonReq));
    }

    @ApiOperation("考勤班组人员列表-（未加入考勤班组的人员）")
    @PostMapping("KqGroupPersonsUn")
    public CallResult<IPage<IscPersonDTO>> KqGroupPersonsUn(@RequestBody IscPersonReq iscPersonReq){
        return BackJson.success(kqDepartmentGroupPersonService.KqGroupPersonsUn(iscPersonReq));
    }

    @ApiOperation("考勤班组人员 - 新增")
    @PostMapping("KqGroupPersonsAdd")
    public CallResult<?> KqGroupPersonsAdd(@RequestBody(required = true) @Valid KqGroupPersonsAddReq kqGroupPersonsAddReq){
        kqDepartmentGroupPersonService.KqGroupPersonsAdd(kqGroupPersonsAddReq);
        return BackJson.success();
    }

    @ApiOperation("考勤班组人员 - 移除")
    @PostMapping("KqGroupPersonsRm")
    public CallResult<?> KqGroupPersonsRm(@RequestBody(required = true) @Valid KqGroupPersonsAddReq kqGroupPersonsAddReq){
        kqDepartmentGroupPersonService.KqGroupPersonsRm(kqGroupPersonsAddReq);
        return BackJson.success();
    }


    // --------------   考勤班组人员管理  end --------------------


    // --------------   考勤点 isc获取   --------------------

    // 考勤点 isc获取,  门禁点位 列表


    // 配置
    @ApiOperation("考勤配置 - 获取")
    @PostMapping("KqConfig")
    public CallResult<KqConfig> KqConfig() {
        return BackJson.success(kqConfigService.KqConfig());
    }

    @ApiOperation("考勤配置 - 配置")
    @PostMapping("KqConfigSet")
    public CallResult<?> KqConfigSet(@RequestBody KqConfig kqConfig) {
        kqConfigService.KqConfigSet(kqConfig);
        return BackJson.success();
    }

    @ApiOperation("考勤配置 - 按组查询脱岗考勤点位")
    @PostMapping("KqConfigGetLeave")
    public CallResult<List<KqConfigGetLeaveDTO>> KqConfigGetLeave(@RequestBody KqConfigGetLeaveReq kqConfigGetLeaveReq) {
        return BackJson.success(kqLocationLeaveService.KqConfigGetLeave(kqConfigGetLeaveReq));
    }

    @ApiOperation("考勤配置 - 脱岗考勤点位和人员分组关联")
    @PostMapping("KqConfigAddLeave")
    public CallResult<?> KqConfigAddLeave(@RequestBody KqConfigSetLeaveReq kqConfigSetLeaveReq) {
        kqLocationLeaveService.KqConfigAddLeave(kqConfigSetLeaveReq);
        return BackJson.success();
    }


    @ApiOperation("考勤配置 - 脱岗考勤点位和人员分组移除关联")
    @PostMapping("KqConfigRmLeave")
    public CallResult<?> KqConfigRmLeave(@RequestBody KqConfigSetLeaveReq kqConfigSetLeaveReq) {
        kqLocationLeaveService.KqConfigRmLeave(kqConfigSetLeaveReq);
        return BackJson.success();
    }


    // 考勤点管理
    @ApiOperation("门禁系统 - 获取门禁点")
    @PostMapping("KqDoors")
    public CallResult<IPage<IscDoorDTO>> KqDoors(@RequestBody IscDoorReq iscDoorReq) {
        return BackJson.success(iscDoorService.KqDoors(iscDoorReq));
    }

    @ApiOperation("人脸抓拍 - 监控相机点位")
    @PostMapping("KqCameras")
    public CallResult<IPage<IscCamera>> KqCameras(@RequestBody IscDoorReq iscDoorReq) {
        return BackJson.success(iscCameraService.KqCameras(iscDoorReq));
    }

    // 厂区位置管理
    @ApiOperation("厂区位置 - 列表")
    @PostMapping("KqLocations")
    public CallResult<IPage<KqLocationDTO>> KqLocations(@RequestBody KqLocationReq kqLocationReq) {
        return BackJson.success(kqLocationService.KqLocations(kqLocationReq));
    }

    @ApiOperation("厂区位置 - 新增")
    @PostMapping("KqLocationAdd")
    public CallResult<?> KqLocationAdd(@RequestBody @Valid KqLocationAddReq kqLocationAddReq) {
        kqLocationService.KqLocationAdd(kqLocationAddReq);
        return BackJson.success();
    }

    @ApiOperation("厂区位置 - 删除")
    @PostMapping("KqLocationDel")
    public CallResult<?> KqLocationDel(@RequestBody @Valid BaseId baseId) {
        kqLocationService.KqLocationDel(baseId);
        return BackJson.success();
    }

    @ApiOperation("厂区位置 - 修改")
    @PostMapping("KqLocationUpd")
    public CallResult<?> KqLocationUpd(@RequestBody @Valid KqLocationUpdReq kqLocationUpdReq) {
        kqLocationService.KqLocationUpd(kqLocationUpdReq);
        return BackJson.success();
    }

    // 考勤分组列表
    @ApiOperation("厂区位置-考勤分组 - 列表(默认数据组)")
    @PostMapping("KqLocationGroup")
    public CallResult<List<KqDoorType>> KqDoorTypes(){
        return BackJson.success(KqDoorTypeEnum.toList());
    }

    // 厂区位置考勤分组管理
    @ApiOperation("厂区位置-考勤分组 - 列表(按厂区位置筛选)")
    @PostMapping("KqLocationGroups")
    public CallResult<IPage<KqLocationGroupDTO>> KqLocationGroups(@RequestBody @Valid KqLocationGroupReq kqLocationGroupReq) {
        return BackJson.success(kqLocationGroupService.KqLocationGroups(kqLocationGroupReq));
    }

    @ApiOperation("厂区位置-考勤分组 - 新增")
    @PostMapping("KqLocationGroupAdd")
    public CallResult<?> KqLocationGroupAdd(@RequestBody @Valid KqLocationGroupAddReq kqLocationGroupAddReq) {
        kqLocationGroupService.KqLocationGroupAdd(kqLocationGroupAddReq);
        return BackJson.success();
    }

    @ApiOperation("厂区位置-考勤分组 - 删除")
    @PostMapping("KqLocationGroupDel")
    public CallResult<?> KqLocationGroupDel(@RequestBody @Valid BaseId baseId) {
        kqLocationGroupService.KqLocationGroupDel(baseId);
        return BackJson.success();
    }

    @ApiOperation("厂区位置-考勤分组 - 修改 (已作废)")
    @PostMapping("KqLocationGroupUpd")
    public CallResult<?> KqLocationGroupUpd(@RequestBody @Valid KqLocationUpdReq kqLocationUpdReq) {
        // kqLocationGroupService.KqLocationGroupUpd(kqLocationUpdReq);
        return BackJson.success();
    }

    // 门禁点分组管理

    @ApiOperation("厂区位置-考勤分组 - 门禁点列表")
    @PostMapping("KqLocationGroupDoors")
    public CallResult<List<KqLocationGroupDoorDTO>> KqLocationGroupDoors(@RequestBody @Valid KqLocationGroupDoorReq kqLocationGroupDoorReq) {
        return BackJson.success(kqLocationGroupDoorService.KqLocationGroupDoors(kqLocationGroupDoorReq));
    }

    @ApiOperation("厂区位置-考勤分组 - 门禁点新增")
    @PostMapping("KqLocationGroupDoorAdd")
    public CallResult<?> KqLocationGroupDoorAdd(@RequestBody @Valid KqLocationGroupDoorAddReq kqLocationGroupDoorAddReq) {
        kqLocationGroupDoorService.KqLocationGroupDoorAdd(kqLocationGroupDoorAddReq);
        return BackJson.success();
    }

    @ApiOperation("厂区位置-考勤分组 - 门禁点移除")
    @PostMapping("KqLocationGroupDoorRm")
    public CallResult<?> KqLocationGroupDoorRm(@RequestBody @Valid KqLocationGroupDoorAddReq kqLocationGroupDoorAddReq) {
        kqLocationGroupDoorService.KqLocationGroupDoorRm(kqLocationGroupDoorAddReq);
        return BackJson.success();
    }


    // 报警查询


    @ApiOperation("考勤报警列表")
    @PostMapping("KqAlarms")
    public CallResult<IPage<KqAlarmDTO>> KqAlarms(@RequestBody KqAlarmReq kqAlarmReq) {
        return BackJson.success(kqAlarmService.KqAlarms(kqAlarmReq));
    }

    @ApiOperation("考勤报警 - 修正")
    @PostMapping("KqAlarmUpd")
    public CallResult<?> KqAlarmUpd(@RequestBody KqAlarmUpdReq kqAlarmUpdReq) {
        kqAlarmService.KqAlarmUpd(kqAlarmUpdReq);
        return BackJson.success();
    }

    @ApiOperation("考勤报警类型列表")
    @PostMapping("KqAlarmTypes")
    public CallResult<List<KqAlarmTypeDTO>> KqAlarmTypes() {
        return BackJson.success(KqAlarmTypeEnum.getList());
    }


    @ApiOperation("考勤报警-导出,get链接方式")
    @RequestMapping(value = "KqAlarmExport", method = RequestMethod.GET)
    public void KqAlarmExport(@ApiParam(name="monStr",value="月份格式2019-07",required=true) @RequestParam(name = "monStr") String monStr
            , HttpServletResponse response) throws ParseException {
        kqAlarmService.KqAlarmExport(monStr, response);
    }


    // 报表输出
    @ApiOperation("考勤报表列表")
    @PostMapping("Kqs")
    public CallResult<IPage<KqDTO>> Kqs(@RequestBody KqReq kqReq) {
        return BackJson.success(kqService.Kqs(kqReq));
    }


    @ApiOperation("考勤报表-修改考勤状态和修正备注")
    @PostMapping("KqUpd")
    public CallResult<?> KqUpd(@RequestBody @Valid KqUpdReq kqUpdReq) {
        kqService.kqService(kqUpdReq);
        return BackJson.success();
    }

    @ApiOperation("考勤报表-导出,get链接方式")
    @RequestMapping(value = "KqExport", method = RequestMethod.GET)
    public void KqExport(@ApiParam(name="startDay",value="格式2019-07-01",required=true) @RequestParam(name = "startDay") String startDay,
                         @ApiParam(name="endDay",value="格式2019-07-02",required=true) @RequestParam(name = "endDay") String endDay
            , HttpServletResponse response) throws ParseException {
        kqService.KqExport(startDay, endDay, response);
    }
}
